package com.ws.universal.tools.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * @创建者 ws
 * @创建时间 2021/9/10
 * @描述 字段保存
 */
public class CacheUtils {
    private static SharedPreferences sp;
    private static Context context;

    public static void initial(Context con) {
        context = con;
        sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
    }


    /**
     * 获取软件保存的参数
     *
     * @param key
     * @return
     */
    public static boolean getBoolean(String key) {

        return sp.getBoolean(key, false);//默认返回false
    }

    /**
     * 保存boolean类型的软件参数
     *
     * @param
     * @param key
     * @param defValue
     */
    public static void setBoolean(String key, boolean defValue) {

        sp.edit().putBoolean(key, defValue).commit();
    }

    /**
     * 缓存数据
     *
     * @param
     * @param key
     * @param defValue
     */
    public static void setString(String key, String defValue) {

        sp.edit().putString(key, defValue).commit();
    }

    public static void clearString(String key, String defValue) {

        sp.edit().putString(key, defValue).commit();
    }

    /**
     * 缓存数据
     *
     * @param
     * @param key
     * @param defValue
     */
    public static void setLong(String key, long defValue) {

        sp.edit().putLong(key, defValue).commit();
    }

    /**
     * 缓存数据
     *
     * @param
     * @param key
     * @param defValue
     */
    public static void setFloat(String key, float defValue) {

        sp.edit().putFloat(key, defValue).commit();
    }


    /**
     * 得到缓存数据
     *
     * @param key
     * @return
     */
    public static String getString(String key) {

        return sp.getString(key, "");//默认返回false
    }

    /**
     * 得到缓存数据
     *
     * @param key
     * @return
     */
    public static float getFloat(String key) {

        return sp.getFloat(key, 1.0f);//默认返回false
    }

    /**
     * 得到缓存数据
     *
     * @param key
     * @return
     */
    public static long getLong(String key) {

        return sp.getLong(key, 0);//默认返回0
    }

    /**
     * 清除数据
     */
    public static void clearData() {

        sp.edit().clear().commit();
    }


    /**
     * @param key
     * @param defValue
     */
    public static void setInt(String key, int defValue) {

        if (defValue != 0) {
            sp.edit().putInt(key, defValue).commit();
        }
    }


    public static int getInt(String key) {

        return sp.getInt(key, 0);//默认返回false
    }

    /**
     *
     */
    public static void setIntClear(String key) {

        sp.edit().putInt(key, 0).commit();
    }
}
